<?php
/**
 * Created by JetBrains PhpStorm.
 * User: user
 * Date: 9/24/13
 * Time: 11:10 PM
 * To change this template use File | Settings | File Templates.
 */

class Linkwebs extends MY_Controller
{

    function __construct(){
        parent::MY_Controller();
        $this->session->set_userdata(array(config_item('session_admin').'menu_current'=>11));
        $this->load->library('login_manager');
    }


    function index(){
        $this->listAll();
    }

    function listAll($offset=0,$limit=20){
        $o = new Linkweb();
        $o->order_by('position');
        $searchKey = "";
        if($_SERVER['METHOD_REQUEST']=='GET'){
        }
        else{
            $searchKey = $this->input->post("search_name");
            $o->distinct();
            $o->group_start();
            $o->like('name_vietnamese',$searchKey);
            $o->group_end();
        }
        $o->get_paged($offset, $limit, TRUE);
        setPagination($this->admin.'levels/listAll/',$o->paged->total_rows,$limit,4);

        $dis['base_url']=base_url();
        $dis['view']='linkweb/list';
        $dis['levels']=$o;
        $dis['searchKey'] = $searchKey;
        $dis['menu_active']="Link web";
        $dis['title_table']="Trang hiện tại:".$o->paged->current_page.'/'.$o->paged->total_pages;
        $dis['title']="Danh sách Link web";
        $dis['nav_menu']=array(
            array(
                "type"=>"add",
                "text"=>"Thêm Links",
                "link"=>"{$this->admin_url}linkwebs/edit/0",
                "onclick"=>""
            )
        );

        $this->viewadmin($dis);
    }

    function edit($id=0){
        $o = new Linkweb($id);
        if($_SERVER['REQUEST_METHOD'] == 'GET'){
        }
        else{
            $o->name_vietnamese = $this->input->post('name_vietnamese');
            $o->name_english = $this->input->post('name_english');
            $o->link = $this->input->post('link');
            if($o->save()){
                flash_message('success', 'Thành công. Thao tác đã được thực hiện.');
                redirect($this->admin.'linkwebs/edit/'.$o->id);
            }
            else{
                flash_message('error', 'Có lỗi. Vui lòng kiểm tra lại');
                redirect($this->admin_url.'linkwebs/listAll');
            }
        }

        $sitelanguage = new Sitelanguage();
        $sitelanguage->order_by('position');
        $sitelanguage->get()->all;
        $dis['sitelanguage'] = $sitelanguage;

        $dis['base_url']=base_url();
        $dis['view']='linkweb/edit';
        $dis['object'] = $o;
        $dis['menu_active']="Link web";
        $dis['title']="Thêm/sửa Link web";
        $dis['nav_menu']=array(
            array(
                "type"=>"back",
                "text"=>"Quay về",
                "link"=>"{$this->admin_url}linkwebs/listAll",
                "onclick"=>""
            )
        );

        $this->viewadmin($dis);
    }

    function delete($id){
        $o = new Linkweb($id);
        if($o->delete()){
            flash_message('success', 'Level đã được xóa thành công');
            redirect($this->admin_url.'linkwebs/listAll');
        }
        else{
            flash_message('error', 'Có lỗi. Vui lòng kiểm tra lại');
            redirect($this->admin_url.'linkwebs/listAll');
        }
    }

    function updatePosition(){
        $positionList = $this->input->post('positionList');
        $idList = $this->input->post('idList');
        $levels = new Linkweb();
        for($i= 0; $i < count($idList) ; $i++)
        {
            $levels->where("id",$idList[$i]);
            $levels->get();

            $levels->position = $positionList[$i];
            $levels->save();
            $levels->clear();
        }
        redirect("admin/linkwebs/listAll/");
    }
}